from django import forms
from django.contrib.auth.forms import UserCreationForm, UserChangeForm
from .models import Readers

from django.contrib.auth.forms import AuthenticationForm
from django.contrib.auth import get_user_model

class ReadersCreationForm(UserCreationForm):
    class Meta(UserCreationForm.Meta):
        model = Readers
        fields = UserCreationForm.Meta.fields + ('student_number', 'contact_info', 'name')
class ReadersChangeForm(UserChangeForm):
    class Meta:
        model = Readers
        fields = ('username', 'email', 'student_number', 'contact_info', 'name')

UserModel = get_user_model()
class ReadersAuthenticationForm(AuthenticationForm):
    username = forms.CharField(label="Username", max_length=63, 
    widget=forms.TextInput(attrs={'class': 'form-control', 
    'placeholder': 'Username'}))
    password = forms.CharField(label="Password", 
    widget=forms.PasswordInput(attrs={'class': 'form-control', 
    'placeholder': 'Password'}))

    def confirm_login_allowed(self, user):
        """
        这个函数⽤来检查⽤户是否被允许登录。
        你可以根据需要在这⾥添加额外的检查。
        """
        if not user.is_active:
            raise forms.ValidationError("This account is currently inactive.")